<template>
  <div class="app-container">
    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      element-loading-text="数据加载中"
      :data="data"
      border
      style="width: 100%"
    >
      <el-table-column align="center" label="序号" width="80">
        <template slot-scope="scope">
          <span>{{ (page - 1) * limit + (scope.$index + 1) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="头像" width="100">
        <template slot-scope="scope">
          <el-image
            style="width: 40px"
            :src="scope.row.avatar"
            fit="contain"
            :preview-src-list="srcList"
          />
        </template>
      </el-table-column>
      <el-table-column align="center" label="昵称">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="内容">
        <template slot-scope="scope">
          <span>{{ scope.row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建日期">
        <template slot-scope="scope">
          <span>{{
            formatDate(scope.row.createDate, "YYYY-MM-DD hh:mm:ss w")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              circle
              @click="deleteMessage(scope.row)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 数据分页 -->
    <el-pagination
      style="margin-top: 30px"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 20]"
      :page-size="limit"
      layout="prev, pager, next, total, ->, sizes, jumper"
      :total="total"
    />
  </div>
</template>

<script>
import { findMessage, delMessage } from "@/api/message";
import { formatDate } from "@/utils/tools";
import { SERVER_URL } from "@/urlConfig";
import fetchDataAndEdit from "@/mixins/fetchDataAndEdit";
export default {
  mixins: [fetchDataAndEdit],
  data() {
    return {
      SERVER_URL,
    };
  },
  computed: {
    srcList() {
      return this.data.map((it) => it.avatar);
    },
  },
  created() {
    this.fetchData();
  },
  methods: {
    // 获取留言列表
    async fetchList() {
      const { data } = await findMessage(this.page, this.limit);
      const { rows, total } = data;
      return { data: rows, total };
    },
    formatDate,
    // 删除留言
    deleteMessage(row) {
      this.$confirm("确认删除该留言信息吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 判断当前是否只有一条数据
          if (this.data.length === 1 && this.page !== 1) {
            this.page = this.page - 1;
          }
          const resp = await delMessage(row.id);
          if (!resp.code) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.fetchData();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
</style>